﻿/* Personal Picture Postcard */

var code, language, mailAddr;

function getLanguage() {
    var lang = $("meta[http-equiv=Content-Language]").attr("content");
    if (!lang) {
        lang = $("meta[httpEquiv=Content-Language]").attr("content");
    }
    switch (lang) {
        case "zh-tw":
            return "chi";
        case "zh-cn":
            return "chis";
        case "en-gb":
            return "eng";
        case "pt":
            return "por";
        default:
            return "eng";
    }
}

function getTxt(desc, lang) {
    switch (desc) {
        case "SEND_TO_FRIEND":
            switch (lang) {
                case "chi":
                    return "電郵給朋友";
                case "chis":
                    return "电邮给朋友";
                case "por":
                    return "Enviar em e-mail";
                case "eng":
                    return "E-mail to Friend";
            }
            break;
        case "DOWNLOAD_IMG":
            switch (lang) {
                case "chi":
                    return "下載圖片";
                case "chis":
                    return "下载图片";
                case "por":
                    return "Descarregar";
                case "eng":
                    return "Download";
            }
            break;
        case "MAIL_SUBJECT":
            switch (lang) {
                case "chi":
                    return "我的照片明信片";
                case "chis":
                    return "我的照片明信片";
                case "por":
                    return "Postal Fotográfico Personalizado";
                case "eng":
                    return "Personal Picture Postcard";
            }
            break;
        case "SEARCH_CONTENT_REQUIRE":
            switch (lang) {
                case "chi":
                    return "請輸入明信片編號";
                case "chis":
                    return "请输入明信片编号";
                case "por":
                    return "Preencha o código do seu postal";
                case "eng":
                    return "Please fill in postcard code";
            }
            break;
        case "IMG_NOT_FOUND":
            switch (lang) {
                case "chi":
                    return "很抱歉，找不到您的明信片。";
                case "chis":
                    return "很抱歉，找不到您的明信片。";
                case "por":
                    return "Desculpe, o número de postal fotográfico não existe.";
                case "eng":
                    return "Sorry, your postcard is not found.";
            }
            break;
        case "EMAIL_REQUIRE":
            switch (lang) {
                case "chi":
                    return "請輸入電郵地址";
                case "chis":
                    return "请输入电邮地址";
                case "por":
                    return "Preencha o endereço de e-mail";
                case "eng":
                    return "Please fill in e-mail address";
            }
            break;
        case "EMAIL_INVALID":
            switch (lang) {
                case "chi":
                    return "請輸入正確的電郵地址";
                case "chis":
                    return "请输入正确的电邮地址";
                case "por":
                    return "Preencha o endereço de e-mail correcto";
                case "eng":
                    return "Please fill in a valid e-mail address";
            }
            break;
        case "SENDER_REQUIRE":
            switch (lang) {
                case "chi":
                    return "請輸入寄件者";
                case "chis":
                    return "请输入寄件者";
                case "por":
                    return "Preencha o remetente";
                case "eng":
                    return "Please fill in sender";
            }
            break;
        case "MAIL_SENDING":
            switch (lang) {
                case "chi":
                    return "郵件傳送中，請稍候...";
                case "chis":
                    return "邮件传送中，请稍候...";
                case "por":
                    return "O postal está a ser enviado. Um momento, por favor...";
                case "eng":
                    return "Mail is sending, Please wait a moment...";
            }
            break;
        case "SEND_SUCCESS":
            switch (lang) {
                case "chi":
                    return "您的明信片已成功地電郵到以下地址： ";
                case "chis":
                    return "您的明信片已成功地电邮到以下地址： ";
                case "por":
                    return "O seu postal foi enviado para o seguinte endereço: ";
                case "eng":
                    return "Your postcard has been successfully sent to the following address: ";
            }
            break;
    }
}

function showRequest(formData, jqForm, options) {
    //    var queryString = $.param(formData);
    //    alert("About to submit: " + queryString);
    //    return true;
}

function showSearchResponse(responseText, statusText, xhr, $form) {
    var html = "";
    switch (responseText) {
        case "TRUE":
            html = '<img src="/asp/postcard/load.asp?code=' + code + '" alt="Postcard" />';
            html += '<ul>';
            html += '<li id="PicPostDownload"><a href="/asp/postcard/download.asp?code=' + code + '">' + getTxt("DOWNLOAD_IMG", language) + '</a></li>';
            html += '<li id="PicPostEmail"><a href="#">' + getTxt("SEND_TO_FRIEND", language) + '</a></li>';
            html += '</ul>';
            $("#PicPostImg").css("background", '#FFFFFF url("/images/icon/ajax-loader1.gif") no-repeat center center');
            break;
        case "FALSE":
            html = "<p>" + getTxt("IMG_NOT_FOUND", language) + "</p>";
            $("#PicPostImg").css("background", '#FFFFFF url("/images/icon/application_warning.png") no-repeat 20px center');
            break;
        default:
            html = "status: " + statusText + "," + "responseText: " + responseText;
            break;
    }
    $("#PicPostImg").html(html);
}

function showMailResponse(responseText, statusText, xhr, $form) {
    var html = "";
    //    alert(responseText + "," + statusText + "," + xhr + "," + $form);
    switch (responseText) {
        case "TRUE":
            html = getTxt("SEND_SUCCESS", language) + mailAddr;
            break;
        case "FALSE":
            html = "status: " + statusText + "," + "responseText: " + responseText;
            break;
        default:
            html = "status: " + statusText + "," + "responseText: " + responseText;
            break;
    }
    $("#PicPostMailConfm").html(html).fadeIn();
}

function submitSearchForm() {
    code = $.trim($("#PicPostCode").val().toUpperCase());
    var options = { beforeSubmit: showRequest, success: showSearchResponse };
    $("#PicPostSearchForm").ajaxSubmit(options);
    $("#PicPostEmailForm").hide();
    if ($("#PicPostMailConfm").length > 0) {
        $("#PicPostMailConfm").hide();
    }
    if ($("#PicPostImg").length <= 0) {
        $("#PicPostSearchForm").after('<div id="PicPostImg"></div>');
    }
}

function submitMailForm() {
    mailAddr = $.trim($("#PicPostMailAddr").val());
    subject = getTxt("MAIL_SUBJECT", language);
    var options = { data: { subject: subject }, beforeSubmit: showRequest, success: showMailResponse, clearForm: true };
    $("#PicPostEmailForm").ajaxSubmit(options);
    $("#PicPostEmailForm").hide();
    if ($("#PicPostMailConfm").length <= 0) {
        $("#PicPostEmailForm").after('<div id="PicPostMailConfm">' + getTxt("MAIL_SENDING", language) + '</div>');
    }
    else {
        $("#PicPostMailConfm").html(getTxt("MAIL_SENDING", language)).fadeIn();
    }
}

$(document).ready(function () {
    var options1 = { submitHandler: submitSearchForm, errorElement: "em" };
    var options2 = { submitHandler: submitMailForm, errorElement: "em" };

    language = getLanguage();

    $("#PicPostSearchForm").validate(options1);
    $("#PicPostCode").rules("add", { required: true, messages: { required: getTxt("SEARCH_CONTENT_REQUIRE", language)} });

    $("#PicPostEmailForm").validate(options2);
    $("#PicPostMailAddr").rules("add", { required: true, email: true, messages: { required: getTxt("EMAIL_REQUIRE", language), email: getTxt("EMAIL_INVALID", language)} });
    $("#PicPostMailSender").rules("add", { required: true, messages: { required: getTxt("SENDER_REQUIRE", language)} });

    $("#PicPostEmail a").live("click", function () {
        if ($("#PicPostMailConfm").length > 0) {
            $("#PicPostMailConfm").hide();
        }
        $("#PicPostEmailForm").fadeIn();
    });
});